package mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/** 
 * 
 * @author Team 4 - IEV2A Hogeschool utrecht
 *
 * Dit is de klasse SendMail.
 * Deze klasse kan een mail van uit de applicatie versturen.
 */

public class SendMail {
	
	private String afzender;
	private String ontvanger;
	private String onderwerp;
	private String text;
	final String username = "osinschrijving@gmail.com";
	final String password = "themaopdracht";
	
	/**
	 * @param afzender - de afzender kan hier door gegeven worden
	 * @param ontvanger - de ontvanger kan hier doorgegeven worden
	 * @param onderwerp - het onderwerp kan hier doorgegeven worden
	 * @param text - de text van het bericht kan hier doorgegeven worden
	 */
 
	public SendMail(String afzender, String ontvanger, String onderwerp, String text){
		this.afzender = afzender;
		this.ontvanger = ontvanger;
		this.onderwerp = onderwerp;
		this.text = text;
	}
 
	/**
	 * De fucntie Send() verstuurd het bericht.
	 */
	public void send(){
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session mailSession = Session.getInstance(props,
				  new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				  });
		Message simpleMessage = new MimeMessage(mailSession);
 
		InternetAddress fromAddress = null;
		InternetAddress toAddress = null;
		try {
			fromAddress = new InternetAddress(afzender);
			toAddress = new InternetAddress(ontvanger);
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		try {
			simpleMessage.setFrom(fromAddress);
			simpleMessage.setRecipient(RecipientType.TO, toAddress);
			simpleMessage.setSubject(onderwerp);
			simpleMessage.setText(text);
 
			Transport.send(simpleMessage);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}